import IPromptModule from "../interface/promptModule";

const path = require("path");

interface IPkg {
  name: string;
  version: string;
  homepage: string;
  main: string;
  license: string;
  scripts: object;
  dependencies: { [key: string]: string };
  devDependencies: { [key: string]: string };
}

class Pkg implements IPkg {
  static _instance = new Pkg();

  static get instance() {
    if (Pkg._instance) {
      return Pkg._instance;
    }
    Pkg._instance = new Pkg();
    return Pkg._instance;
  }

  public name = "";

  public version = "1.0.0";

  public homepage = ".";

  public main = "index.js";

  public license: string = "MIT";

  public scripts = {}

  public dependencies: { [key: string]: string } = {};

  public devDependencies: { [key: string]: string } = {};

  /**
   * 注入dependencies、devDenpendencies
   * @param name 特性名
   */
  injectPrompt(name: string) {
    const data: IPromptModule = require(path.join(__dirname, `../prompt/${name}.js`));
    this.dependencies = Object.assign({}, this.dependencies, data.dependencies);
    this.devDependencies = Object.assign({}, this.devDependencies, data.devDependencies);
  }

  toJSON() {
    const json: Partial<IPkg> = {};

    json.name = this.name;
    json.version = this.version;
    json.homepage = this.homepage;
    json.main = this.main;
    json.license = this.license;
    json.scripts = this.scripts;
    json.dependencies = this.dependencies;
    json.devDependencies = this.devDependencies;
    return json;
  }
}

export  default Pkg;
